package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;

import com.mall.order.CancelOrderService;
import com.mall.order.OrderDeleteService;
import com.mall.order.OrderQueryService;

import com.mall.order.OrderCoreService;
import com.mall.order.OrderDeleteService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;

import com.mall.order.dto.*;
import com.mall.shopping.ITestProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;

/**
 * @Description:
 * @author: Aaron
 * @create: 2021-06-18 15:52
 **/
@Slf4j
@RestController
@RequestMapping("/shopping")
public class OrderController {
    @Reference(interfaceClass = OrderQueryService.class, timeout = 3000, retries = 0, check = false)
    OrderQueryService orderQueryService;

    @Reference(interfaceClass = OrderDeleteService.class, timeout = 3000, retries = 0, check = false)
    OrderDeleteService orderDeleteService;


    @Reference(interfaceClass = CancelOrderService.class,timeout = 3000,retries = 0, check = false)
    CancelOrderService cancelOrderService;

    @Reference(timeout = 3000,check = false)
    private OrderCoreService orderCoreService;

    //Aaron
    @PostMapping("/order")
    public ResponseData order(@RequestBody CreateOrderRequest request,HttpServletRequest servletRequest) {
        String userInfo = (String)servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long userId = Long.parseLong(object.get("uid").toString());

        request.setUserId(userId);
        request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response = orderCoreService.createOrder(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }

        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }




    //Aaron
    @GetMapping("/order")
    public ResponseData queryOrder(OrderListRequest request, HttpServletRequest servletRequest) {
        String userInfo = (String)servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long userId = Long.parseLong(object.get("uid").toString());
        request.setSort("create_time");
        request.setUserId(userId);
        OrderListResponse response = orderQueryService.queryOrder(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/order/{id}")
    @Anoymous
    public ResponseData deleteOrder(@PathVariable String id){

        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);

        DeleteOrderResponse deleteOrderResponse = orderDeleteService.orderDelete(deleteOrderRequest);

        if (deleteOrderResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(deleteOrderResponse.getMsg());
        }

        return new ResponseUtil<>().setErrorMsg(deleteOrderResponse.getMsg());


    }

    //取消订单 zpf
    @PostMapping("cancelOrder")
    public ResponseData postCancelOrder(@RequestBody PostCancelOlderRequest request){
        CancelOrderResponse response = cancelOrderService.cancelOrder(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("/order/{id}")
    public ResponseData selectOrderDetail(@PathVariable String id){

        OrderDetailRequest detailRequest = new OrderDetailRequest();
        detailRequest.setOrderId(id);
        OrderDetailResponse response = orderQueryService.orderDetail(detailRequest);
        if(!OrderRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil<>().setErrorMsg(response.getCode());
        }
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setUserId(response.getUserId());
        orderDetail.setUserName(response.getBuyerNick());
        orderDetail.setOrderStatus(response.getStatus());
        orderDetail.setOrderTotal(response.getPayment().doubleValue());
        orderDetail.setGoodsList(response.getOrderItemDto());
        return new ResponseUtil<>().setData(orderDetail);
    }

}
